<?php
namespace app\agent\controller;

use app\admin\widget\Name;
use app\common\cache\common\IAreaCache;
use app\common\cache\insurance\IInsuranceAgencyCache;
use app\common\cache\insurance\IInsuranceCache;
use app\common\cache\insurance\IInsuranceFavoriteCache;
use app\common\cache\insurance\IInsuranceRecommendCache;
use app\common\cache\insurance\IInsuranceTagCache;
use app\common\controller\AgentBaseController;
use app\common\enum\InsuranceStatus;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\ILog;
use app\common\ext\IRequest;
use think\Db;

class Insurance extends AgentBaseController{

    /**
     * @功能：已上架保险列表
     * @开发者：cxl
     * @return string
     */
    public function insurance_up_list(){
        //接收参数
        $insurance_agency_type = IRequest::get('insurance_agency_type');
        $insurance_type = IRequest::get('insurance_type');
        $insurance_agency_id = IRequest::get('insurance_agency_id');
        $insurance_product_id = IRequest::get('insurance_product_id');
        $insurance_product_name = IRequest::get('insurance_product_name');

        //设置查询条件
        $insurance_product_where = null;
        $insurance_product_where['agent_id'] = get_login_agent_id();
        $insurance_product_where['is_delete'] = IsDelete::No;
        $insurance_product_where['insurance_status'] = InsuranceStatus::release;
        if(!empty($insurance_agency_type)) $insurance_product_where['insurance_agency_type'] = $insurance_agency_type;
        if(!empty($insurance_type)) $insurance_product_where['insurance_type'] = $insurance_type;
        if(!empty($insurance_agency_id)) $insurance_product_where['insurance_agency_id'] = $insurance_agency_id;
        if(!empty($insurance_product_id)) $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        if(!empty($insurance_product_name)) $insurance_product_where['insurance_product_name'] = array("like","%{$insurance_product_name}%");

        //取得管理员详情
        $insurance_list = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbOrder("insurance_product_id desc")->pag();
        if ($insurance_list === false){
            error('获取保险列表失败');
        }

        //取得保险机构数据
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //页面赋值
        $this->assign("insurance_agency_type",$insurance_agency_type);
        $this->assign("insurance_agency_id",$insurance_agency_id);
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_product_name",$insurance_product_name);
        $this->assign("insurance_agency_list",$insurance_agency_list);
        $this->assign("insurance_type",$insurance_type);
        $this->assign("insurance_list",$insurance_list);

        //渲染模板输出
        return view("insurance_up_list");
    }

    /**
     * @功能：编辑中保险产品列表
     * @开发者：cxl
     * @return string
     */
    public function insurance_edit_list(){
        //接收参数
        $insurance_agency_type = IRequest::get('insurance_agency_type');
        $insurance_type = IRequest::get('insurance_type');
        $insurance_agency_id = IRequest::get('insurance_agency_id');
        $insurance_product_id = IRequest::get('insurance_product_id');
        $insurance_product_name = IRequest::get('insurance_product_name');
        $insurance_status = IRequest::get('insurance_status');

        //设置查询条件
        $insurance_product_where = null;
        $insurance_product_where['agent_id'] = get_login_agent_id();
        $insurance_product_where['is_delete'] = IsDelete::No;
        if(!empty($insurance_agency_type)) $insurance_product_where['insurance_agency_type'] = $insurance_agency_type;
        if(!empty($insurance_type)) $insurance_product_where['insurance_type'] = $insurance_type;
        if(!empty($insurance_agency_id)) $insurance_product_where['insurance_agency_id'] = $insurance_agency_id;
        if(!empty($insurance_product_id)) $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        if(!empty($insurance_product_name)) $insurance_product_where['insurance_product_name'] = array("like","%{$insurance_product_name}%");
        if(!empty($insurance_status) && !in_array($insurance_status,array(InsuranceStatus::release))) $insurance_product_where['insurance_status'] = $insurance_status;

        //取得保险列表
        $insurance_list = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbOrder("insurance_product_id desc")->pag();
        if ($insurance_list === false){
            error('获取保险列表失败');
        }

        //取得保险机构数据
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //页面赋值
        $this->assign("insurance_agency_type",$insurance_agency_type);
        $this->assign("insurance_type",$insurance_type);
        $this->assign("insurance_agency_id",$insurance_agency_id);
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_product_name",$insurance_product_name);
        $this->assign("insurance_status",$insurance_status);
        $this->assign("insurance_agency_list",$insurance_agency_list);
        $this->assign("insurance_list",$insurance_list);

        //渲染模板输出
        return view("insurance_edit_list");
    }

    /**
     * @功能：已删除保险列表
     * @开发者：cxl
     * @return string
     */
    public function insurance_del_list(){
        //接收参数
        $insurance_agency_type = IRequest::get('insurance_agency_type');
        $insurance_type = IRequest::get('insurance_type');
        $insurance_agency_id = IRequest::get('insurance_agency_id');
        $insurance_product_id = IRequest::get('insurance_product_id');
        $insurance_product_name = IRequest::get('insurance_product_name');

        //设置查询条件
        $insurance_product_where['agent_id'] = get_login_agent_id();
        $insurance_product_where['is_delete'] = IsDelete::Yes;
        if(!empty($insurance_agency_type)) $insurance_product_where['insurance_agency_type'] = $insurance_agency_type;
        if(!empty($insurance_type)) $insurance_product_where['insurance_type'] = $insurance_type;
        if(!empty($insurance_agency_id)) $insurance_product_where['insurance_agency_id'] = $insurance_agency_id;
        if(!empty($insurance_product_id)) $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        if(!empty($insurance_product_name)) $insurance_product_where['insurance_product_name'] = array("like","%{$insurance_product_name}%");

        //取得管理员详情
        $insurance_list = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbOrder("insurance_product_id desc")->pag();
        if ($insurance_list === false){
            error('获取保险列表失败');
        }

        //取得保险机构数据
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //页面赋值
        $this->assign("insurance_agency_type",$insurance_agency_type);
        $this->assign("insurance_agency_id",$insurance_agency_id);
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_product_name",$insurance_product_name);
        $this->assign("insurance_agency_list",$insurance_agency_list);
        $this->assign("insurance_type",$insurance_type);
        $this->assign("insurance_list",$insurance_list);

        //渲染模板输出
        return view("insurance_del_list");
    }

    /**
     * @功能：保险详情
     * @开发者：cxl
     * @return string
     */
    public function insurance_info(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //取得保险机构表
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得保险信息
        $insurance_info = IInsuranceCache::getInfo($insurance_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($insurance_info)){
            //整理数据
            $insurance_info['insurance_detail'] = stripslashes($insurance_info['insurance_detail']);
            $insurance_info['insurance_feature'] = stripslashes($insurance_info['insurance_feature']);
            $insurance_info['insurance_notice'] = stripslashes($insurance_info['insurance_notice']);
            $insurance_info['insurance_remind'] = stripslashes($insurance_info['insurance_remind']);

            //取得所在区域信息
            $insurance_product_area_where = null;
            $insurance_product_area_where['insurance_product_id'] = $insurance_product_id;
            $insurance_product_area_where['province'] = array("exp","is not null");
            $insurance_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('insurance_product_area')->setDbFiled("province,city")->setDbWhere($insurance_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_info",$insurance_info);
        $this->assign("area_list",$area_list);
        $this->assign("insurance_agency_list",$insurance_agency_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：保险日志
     * @开发者：cxl
     * @return string
     */
    public function insurance_log(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //取得日志
        $insurance_product_log_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_log_list = IDb::getInstance('insurance_product_log')->setDbWhere($insurance_product_log_where)->setDbOrder("insurance_product_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("insurance_product_log_list",$insurance_product_log_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加保险
     * @开发者：cxl
     * @return string
     */
    public function insurance_add(){
        //取得保险机构表
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("area_list",$area_list);
        $this->assign("insurance_agency_list",$insurance_agency_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：修改保险
     * @开发者：cxl
     * @return string
     */
    public function insurance_edit(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //取得保险机构表
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);;

        //取得保险信息
        $insurance_info = IInsuranceCache::getInfo($insurance_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($insurance_info)){
            //整理数据
            $insurance_info['insurance_tag_ary'] = explode(",",$insurance_info['insurance_tag_str']);
            $insurance_info['insurance_detail'] = stripslashes($insurance_info['insurance_detail']);
            $insurance_info['insurance_feature'] = stripslashes($insurance_info['insurance_feature']);
            $insurance_info['insurance_notice'] = stripslashes($insurance_info['insurance_notice']);
            $insurance_info['insurance_remind'] = stripslashes($insurance_info['insurance_remind']);



            //取得所在区域信息
            $insurance_product_area_where = null;
            $insurance_product_area_where['insurance_product_id'] = $insurance_product_id;
            $insurance_product_area_where['province'] = array("exp","is not null");
            $insurance_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('insurance_product_area')->setDbFiled("province,city")->setDbWhere($insurance_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //页面赋值
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_info",$insurance_info);
        $this->assign("area_list",$area_list);
        $this->assign("insurance_agency_list",$insurance_agency_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加保险
     * @开发者：cxl
     * @return string
     */
    public function insurance_add_submit(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id");
        $insurance_agency_type = IRequest::get("insurance_agency_type",IRequest::NOT_EMPTY,"保险机构类型编号不能为空！");
        $insurance_agency_id = IRequest::get("insurance_agency_id",IRequest::NOT_EMPTY,"保险机构编号不能为空！");
        $insurance_product_name = IRequest::get("insurance_product_name",IRequest::NOT_EMPTY,"保险产品名称不能为空！");
        $insurance_product_name_py = IRequest::get("insurance_product_name_py",IRequest::NOT_EMPTY,"保险产品英文名称不能为空！");
        $insurance_product_outline = IRequest::get("insurance_product_outline",IRequest::NOT_EMPTY,"保险产品概要不能为空！");
        $insurance_type = IRequest::get("insurance_type",IRequest::NOT_EMPTY,"保险类型不能为空！");
        $add_list = IRequest::get("add_list");
        $area = IRequest::get("area");
        $insurance_detail = IRequest::get("insurance_detail",IRequest::NOT_EMPTY,"保险详情不能为空！");
        $insurance_feature = IRequest::get("insurance_feature",IRequest::NOT_EMPTY,"产品特色不能为空！");
        $insurance_notice = IRequest::get("insurance_notice",IRequest::NOT_EMPTY,"投标须知不能为空！");
        $insurance_remind = IRequest::get("insurance_remind",IRequest::NOT_EMPTY,"特别提醒不能为空！");
        $seo_keywords = IRequest::get("seo_keywords");
        $seo_description = IRequest::get("seo_description");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");


        //处理自定义标签
        if (strlen($add_list)>=2){
            $add_list = substr($add_list,0,strlen($add_list)-1);
        }else{
            $add_list = null;
        }

        //开启事物处理
        IDb::dbStartTrans();

        //判断是否为新添加数据
        if(empty($insurance_product_id)) {
            //添加保险
            $insurance_data = null;
            $insurance_data['agent_id'] = get_login_agent_id();
            $insurance_data['agent_admin_id'] = get_login_agent_admin_id();
            $insurance_data['insurance_agency_type'] = $insurance_agency_type;
            $insurance_data['insurance_agency_id'] = $insurance_agency_id;
            $insurance_data['insurance_product_name'] = $insurance_product_name;
            $insurance_data['insurance_product_name_py'] = $insurance_product_name_py;
            $insurance_data['insurance_product_outline'] = $insurance_product_outline;
            $insurance_data['insurance_type'] = $insurance_type;
            $insurance_data['insurance_tag_str'] = $add_list;
            $insurance_data['insurance_detail'] = $insurance_detail;
            $insurance_data['insurance_feature'] = $insurance_feature;
            $insurance_data['insurance_notice'] = $insurance_notice;
            $insurance_data['insurance_remind'] = $insurance_remind;
            $insurance_data['seo_keywords'] = $seo_keywords;
            $insurance_data['seo_description'] = $seo_description;
            $insurance_data['order_by'] = intval($order_by);
            $insurance_data['remark'] = $remark;
            $insurance_data['create_time'] = get_date_time();
            $insurance_data['is_delete'] = IsDelete::No;
            $insurance_add = IDb::getInstance('insurance_product')->setDbData($insurance_data)->add();
            if ($insurance_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加保险基础信息失败，请联系管理员！");
            }

            //添加操作日志
            $insurance_log_add = ILog::InsuranceLog($insurance_add,null,get_login_agent_admin_id(),"创建保险！");
            if ($insurance_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加保险基础信息失败，请联系管理员！");
            }

            //设置保险编号
            $insurance_product_id = $insurance_add;
        }else{
            //添加保险
            $insurance_data = null;
            $insurance_where = null;
            $insurance_where['insurance_product_id'] = $insurance_product_id;
            $insurance_data['insurance_agency_type'] = $insurance_agency_type;
            $insurance_data['insurance_agency_id'] = $insurance_agency_id;
            $insurance_data['insurance_product_name'] = $insurance_product_name;
            $insurance_data['insurance_product_name_py'] = $insurance_product_name_py;
            $insurance_data['insurance_product_outline'] = $insurance_product_outline;
            $insurance_data['insurance_type'] = $insurance_type;
            $insurance_data['insurance_tag_str'] = $add_list;
            $insurance_data['insurance_detail'] = $insurance_detail;
            $insurance_data['insurance_feature'] = $insurance_feature;
            $insurance_data['insurance_notice'] = $insurance_notice;
            $insurance_data['insurance_remind'] = $insurance_remind;
            $insurance_data['seo_keywords'] = $seo_keywords;
            $insurance_data['seo_description'] = $seo_description;
            $insurance_data['order_by'] = intval($order_by);
            $insurance_data['remark'] = $remark;
            $insurance_data['update_time'] = get_date_time();
            $insurance_data['is_delete'] = IsDelete::No;
            $insurance_upd = IDb::getInstance('insurance_product')->setDbData($insurance_data)->setDbWhere($insurance_where)->upd();
            if ($insurance_upd === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加保险基础信息失败，请联系管理员！");
            }

            //添加操作日志
            $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"修改保险！");
            if ($insurance_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加保险基础信息失败，请联系管理员！");
            }
        }

        //删除历史效区域数据
        $insurance_area_where['insurance_product_id'] = $insurance_product_id;
        $insurance_area_del = IDb::getInstance('insurance_product_area')->setDbWhere($insurance_area_where)->del();
        if ($insurance_area_del === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("设置保险有效区域失败！");
        }

        //判断保险有效区域
        if(is_array($area) && count($area) > 0){
            //去掉重复数据
            $area = array_unique($area);

            //循环写数据
            foreach($area as $item){
                //拆分省份及城市
                list($province,$city) = explode(",",$item);
                if(empty($province) || empty($city)){
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("设置保险有效区域失败！");
                }

                //写有效区域数据
                $insurance_area_data = null;
                $insurance_area_data['insurance_product_id'] = $insurance_product_id;
                $insurance_area_data['province'] = $province;
                $insurance_area_data['city'] = $city;
                $insurance_area_data['create_time'] = get_date_time();
                $insurance_area_add = IDb::getInstance('insurance_product_area')->setDbData($insurance_area_data)->add();
                if ($insurance_area_add === false) {
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("设置保险有效区域失败！");
                }
            }
        }else{
            //写有效区域数据
            $insurance_area_data = null;
            $insurance_area_data['insurance_product_id'] = $insurance_product_id;
            $insurance_area_data['province'] = null;
            $insurance_area_data['city'] = null;
            $insurance_area_data['create_time'] = get_date_time();
            $insurance_area_add = IDb::getInstance('insurance_product_area')->setDbData($insurance_area_data)->add();
            if ($insurance_area_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("设置保险有效区域失败！");
            }
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("insurance_edit_list"));
    }

    /**
     * @功能：提交审核
     * @开发者：cxl
     * @return string
     */
    public function insurance_commit(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //开启事物
        IDb::dbStartTrans();

        //修改管理员信息
        $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_data['insurance_status'] = insuranceStatus::audit;
        $insurance_product_data['update_time'] = get_date_time();
        $insurance_upd = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbData($insurance_product_data)->upd();
        if($insurance_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险提交审核失败！");
        }

        //添加操作日志
        $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"保险提交审核！");
        if ($insurance_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险提交审核失败！");
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：保险审核
     * @开发者：cxl
     * @return string
     */
    public function insurance_audit(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //取得保险机构表
        $insurance_agency_list = IInsuranceAgencyCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得保险信息
        $insurance_info = IInsuranceCache::getInfo($insurance_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($insurance_info)){
            //整理数据
//            $insurance_info['insurance_apply_condition_str'] = json_decode($insurance_info['insurance_apply_condition_str'],true);
//            $insurance_info['insurance_apply_material_str'] = json_decode($insurance_info['insurance_apply_material_str'],true);
//            $insurance_info['rate_info'] = json_decode($insurance_info['rate_info'],true);

            //整理币种
//            $insurance_info['insurance_house_list'] = explode(",",$insurance_info['insurance_house_list']);
//            $insurance_info['insurance_identity_list'] = explode(",",$insurance_info['insurance_identity_list']);

            //取得所在区域信息
            $insurance_product_area_where = null;
            $insurance_product_area_where['insurance_product_id'] = $insurance_product_id;
            $insurance_product_area_where['province'] = array("exp","is not null");
            $insurance_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('insurance_product_area')->setDbFiled("province,city")->setDbWhere($insurance_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //页面赋值
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_info",$insurance_info);
        $this->assign("area_list",$area_list);
        $this->assign("insurance_agency_list",$insurance_agency_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：保险审核提交
     * @开发者：cxl
     * @return string
     */
    public function insurance_audit_submit(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");
        $insurance_status = IRequest::get("insurance_status",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //开启事物
        IDb::dbStartTrans();

        //修改管理员信息
        $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_data['insurance_status'] = $insurance_status;
        $insurance_product_data['update_time'] = get_date_time();
        $insurance_upd = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbData($insurance_product_data)->upd();

        if($insurance_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险审核错误1！");
        }

        //添加操作日志
        $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"保险审核！");
        if ($insurance_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险审核错误2！");
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceCache::deletePcList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("insurance_edit_list"));
    }

    /**
     * @功能：保险下架
     * @开发者：cxl
     * @return string
     */
    public function insurance_down(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_data['insurance_status'] = insuranceStatus::down;
        $insurance_product_data['update_time'] = get_date_time();
        $insurance_upd = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbData($insurance_product_data)->upd();
        if($insurance_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("保险下架失败！");
        }

        //修改管理员信息
        $insurance_recommend_where['insurance_product_id'] = $insurance_product_id;
        $insurance_recommend_data['is_delete'] = IsDelete::Yes;
        $insurance_recommend_data['update_admin'] = get_login_agent_admin_id();
        $insurance_recommend_data['update_time'] = get_date_time();
        $insurance_recommend_upd = IDb::getInstance('insurance_recommend')->setDbWhere($insurance_recommend_where)->setDbData($insurance_recommend_data)->upd();
        if($insurance_recommend_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("保险推荐删除失败！");
        }

        //添加操作日志
        $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"保险下架！");
        if ($insurance_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险下架失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceCache::deletePcList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：保险删除
     * @开发者：cxl
     * @return string
     */
    public function insurance_del(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_data['is_delete'] = IsDelete::Yes;
        $insurance_product_data['update_time'] = get_date_time();
        $insurance_upd = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbData($insurance_product_data)->upd();
        if($insurance_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("保险删除失败！");
        }

        //修改管理员信息
        $insurance_recommend_where['insurance_product_id'] = $insurance_product_id;
        $insurance_recommend_data['is_delete'] = IsDelete::Yes;
        $insurance_recommend_data['update_admin'] = get_login_agent_admin_id();
        $insurance_recommend_data['update_time'] = get_date_time();
        $insurance_recommend_upd = IDb::getInstance('insurance_recommend')->setDbWhere($insurance_recommend_where)->setDbData($insurance_recommend_data)->upd();
        if($insurance_recommend_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("保险删除失败！");
        }

        //添加操作日志
        $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"保险删除！");
        if ($insurance_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险删除失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：保险还原
     * @开发者：cxl
     * @return string
     */
    public function insurance_recycle(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_data['is_delete'] = IsDelete::No;
        $insurance_product_data['update_time'] = get_date_time();
        $insurance_upd = IDb::getInstance('insurance_product')->setDbWhere($insurance_product_where)->setDbData($insurance_product_data)->upd();
        if($insurance_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险还原失败！");
        }

        //添加操作日志
        $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"保险还原！");
        if ($insurance_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保险还原失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：保险编辑标签
     * @开发者：cxl
     * @return string
     */
    public function insurance_tag(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");

        //取得标签列表
        $insurance_tag_list = IInsuranceTagCache::getList();
        if($insurance_tag_list === false){
            error("取得标签列表失败！");
        }

        //取得标签
        $insurance_product_info = IInsuranceCache::getInfo($insurance_product_id);
        if($insurance_product_info === false){
            error("取得标签列表失败！");
        }

        //整理标签信息
        $insurance_tag = array();
        if(!empty($insurance_product_info['insurance_tag_str'])){
            $insurance_tag = explode(",",$insurance_product_info['insurance_tag_str']);
        }

        //整理自定义标签
        if (!empty($insurance_product_info['insurance_ext_tag_str'])){
            $insurance_product_info['insurance_ext_tag_ary'] = explode(",",$insurance_product_info['insurance_ext_tag_str']);
        }

        //页面赋值
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_product_info",$insurance_product_info);
        $this->assign("insurance_tag_list",$insurance_tag_list);
        $this->assign("insurance_tag",$insurance_tag);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：保险编辑标签
     * @开发者：cxl
     * @return string
     */
    public function insurance_tag_submit(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"保险编号不能为空！");
        $insurance_tag_array = IRequest::get("insurance_tag_id");
        $add_list = IRequest::get("add_list");

        //处理自定义标签
        if (strlen($add_list)>=2){
            $add_list = substr($add_list,0,strlen($add_list)-1);
        }else{
            $add_list = null;
        }

        //判断是是否为数组
        if(!is_array($insurance_tag_array) || empty($insurance_tag_array)){
            error("请选择保险标签！");
        }

        //开启事物出来
        IDb::dbStartTrans();

        //添加新标签列表
        $insurance_product_where['insurance_product_id'] = $insurance_product_id;
        $insurance_product_data['insurance_ext_tag_str'] = $add_list;
        $insurance_product_data['insurance_tag_str'] = join(",",$insurance_tag_array);
        $insurance_product_data['update_time'] = get_date_time();
        $insurance_product_upd = IDb::getInstance('insurance_product')->setDbData($insurance_product_data)->setDbWhere($insurance_product_where)->upd();
        if($insurance_product_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保存保险标签失败！");
        }

        //添加操作日志
        $insurance_log_add = ILog::InsuranceLog($insurance_product_id,null,get_login_agent_admin_id(),"修改保险标签！");
        if ($insurance_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("修改保险标签失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        IInsuranceCache::deleteInfo($insurance_product_id);
        IInsuranceCache::deleteList();
        IInsuranceRecommendCache::deleteList();
        IInsuranceFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("insurance_edit_list"));
    }

    /**
     * @功能：保险产品分享记录
     * @开发者：cxl
     * @return string
     */
    public function insurance_share_log(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $insurance_product_id = IRequest::get("insurance_product_id");

        //取得数据
        $insurance_share_where = null;
        $insurance_share_where['cp.agent_id'] = get_login_agent_id();
        if(!empty($user_id)) $insurance_share_where['cs.user_id'] = $user_id;
        if(!empty($insurance_product_id)) $insurance_share_where['cs.insurance_product_id'] = $insurance_product_id;
        $insurance_share_list = IDb::getInstance("insurance_share as cs")
            ->setDbFiled('cs.*')
            ->setDbJoin('insurance_product as cp', 'cp.insurance_product_id = cs.insurance_product_id')
            ->setDbWhere($insurance_share_where)
            ->setDbOrder('cs.share_id desc')
            ->pag();
        if ($insurance_share_list === false){
            error('获取贷款分享记录失败');
        }

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_share_list",$insurance_share_list);

        //渲染模板输出
        return view();
    }
}
